package com.amado.course36ByteStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;

public class S09ObjectOutputStreamTest{

	public static void main(String[] args) {
		File file = new File("D:\\temp44\\student.txt");
		List<Student> studentList = Arrays.asList(new Student("李四", 14), new Student("王五", 12), new Student("小明", 12),
				new Student("张三", 12));
		writeFIle(studentList,file);
	}
	
	public static void writeFIle(List<Student> studentList,File file) {
		
		ObjectOutput output = null;
		try {
			
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			if(!file.exists()){
				file.createNewFile();
			}
			output = new ObjectOutputStream(new FileOutputStream(file));
			System.out.println(studentList);
			output.writeObject(studentList);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				output.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
